#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Mikan Storage Module

Mikan Python Web Framework
"""

__author__ = "K.Sonohara"
__status__ = "develop"
__version__ = "0.0.0_36"
__date__    = "2014/01/05"
__license__ = 'LGPL'

# ------------------------------------------------------------
## Import Section
# ------------------------------------------------------------
# Python
from os import path
import codecs
import dircache

# ESiPC
from esipc import ESiPCProperty

# Mikan
from mikan.core import AbstractMikan

# ------------------------------------------------------------
## Variable Section
# ------------------------------------------------------------

# ------------------------------------------------------------
## Function Section
# ------------------------------------------------------------

# ------------------------------------------------------------
## Class Section
# ------------------------------------------------------------

# ------------------------------------------------------------
## Mikan ストレージ抽象 クラス
#
class AbstractMikanStorage(AbstractMikan):

	# ----------------------------------------------------
	## コンストラクタ
	def __init__(self):
		super(AbstractMikanStorage, self).__init__()

	def is_readonly(self):
		return True

	def read(self, filename, encode=None):
		return None

	def write(self, filename, encode=None):
		return None

	def list(self):
		return []

	# ----------------------------------------------------
	## Property
	readonly = ESiPCProperty(is_readonly, None, None)

# ------------------------------------------------------------
## Mikan 基本ストレージ抽象 クラス
#
class AbstractMikanBasicStorage(AbstractMikanStorage):

	# ----------------------------------------------------
	## コンストラクタ
	def __init__(self, readonly=False):
		super(AbstractMikanBasicStorage, self).__init__()

		self.FReadOnly = readonly

	def set_readonly(self, value):
		self.FReadOnly = value

	def is_readonly(self):
		return self.FReadOnly

	def get_base_path(self):
		return None

	def convert_filename(self, subpath, filename):
		p = subpath
		if self.basepath:
			p = path.join(self.basepath, subpath)
		return path.join(p, filename)

	def size(self, subpath, filename):
		f = self.convert_filename(subpath, filename)
		return path.getsize(f)

	def time(self, subpath, filename):
		f = self.convert_filename(subpath, filename)
		return path.getctime(f)

	def read(self, subpath, filename, encode=None):
		f = self.convert_filename(subpath, filename)

		if (encode != None):
			return codecs.open(f, "r", encode)
		else:
			return open(f, "rb")

	def write(self, subpath, filename, encode=None):
		if self.isreadonly:
			return None

		f = self.convert_filename(subpath, filename)
		if (encode != None):
			return codecs.open(f, "w", encode)
		else:
			return open(f, "wb")

	def list_filter(self, subpath, filters):
		l = filters[:]

		for f in list:
			if not path.isfile(self.convertFileName(subpath, f)):
				l.remove(f)

		return l

	def list(self, subpath):
		p = subpath
		if self.basepath:
			p = path.join(self.basepath, subpath)

		l = dircache.listdir(p)
		return self.list_filter(l)

	# ----------------------------------------------------
	## Property
	basepath = ESiPCProperty(get_base_path, None, None)
	isreadonly = ESiPCProperty(is_readonly, set_readonly, None)

# ------------------------------------------------------------
## Mikan ローカルストレージ クラス
#
class MikanLocalStorage(AbstractMikanBasicStorage):

	# ----------------------------------------------------
	## コンストラクタ
	def __init__(self, basepath, readonly=False):
		super(MikanLocalStorage, self).__init__(readonly)

		self.FBasePath = basepath

	def get_base_path(self):
		return self.FBasePath

	# ----------------------------------------------------
	## Property

# ------------------------------------------------------------
## Main Section
# ------------------------------------------------------------
if __name__ == '__main__':
	pass

# ------------------------------------------------------------
